<?php

class Model_FG extends Model {

    function __construct() {
        $this->Lookup = getModel('Model_Lookup');
        $this->Product = getModel('Model_Product');
        $this->fg_images = "/images/fg_images/";
        $this->pic_num = 1;
    }

    function get_fg_product($h_param = array()) {
        $result = array();
        $vote_option_json_array = array();

        $pid = intval($h_param['pid']) ? intval($h_param['pid']) : 0;
        $num = intval($h_param['num']) ? intval($h_param['num']) : 0;
        $cid = intval($h_param['cid']) ? intval($h_param['cid']) : 0;
        $big_cid = intval($h_param['big_cid']) ? intval($h_param['big_cid']) : 0;
        $page = intval($h_param['page']) ? intval($h_param['page']) : 1;
        $size = intval($h_param['size']) ? intval($h_param['size']) : 5;

        $fg_vote_option_typearr = array();
        $fg_vote_option_type = $this->Lookup->getLookup('fg_vote_option_type');

        if ($fg_vote_option_type['result']) {
            foreach ($fg_vote_option_type['result'] as $value) {
                $fg_vote_option_typearr[$value['id']] = $value['cname'];
            }
        }

        $brandarr = array();
        $brand = $this->Lookup->getLookup('brand');
        if ($brand['result']) {
            foreach ($brand['result'] as $value) {
                $brandarr[$value['id']] = $value['name'];
            }
        }
        $where = "TB1.status=1 and TB1.pid<>''";
        if ($pid) {
            $where .=" and TB1.pid=" . $pid;
        } elseif ($num) {
            $where .=" and TB1.num=" . $num;
        }

        if ($cid) {
            $where .=" and TB2.product_layout_category=" . $cid;
        }

        if ($big_cid) {

            $param1 = array();
            $param1['field'] = "id,name,cname";
            $param1['where'] = "status=1 and topid = " . $big_cid;
            $param1['order'] = "id asc"; //"id asc"
            $param1['group'] = "";
            $param1['limit'] = "";
            $param1['pagesize'] = "";
            $param1['page'] = "";
            $param1['join'] = "";
            $param1['on'] = "";
            $data_category = db_query_hash('product_layout_category', $param1);
            $result_category = array();
            if ($data_category['result']) {
                foreach ($data_category['result'] as $value_category) {
                    $result_category[] = $value_category['id'];
                }
            }
            $ids = implode(',', $result_category);
            $where .=" and TB2.product_layout_category in (" . $ids . ")";
        }


        $param = array();
        $param['field'] = "TB1.*,TB2.brand,TB2.cname,TB2.name,TB2.cover_image1,TB2.member_type,TB2.product_layout_category,TB2.price AS itrial_price,TB2.capacity_trial,TB2.attribute_value1";
        $param['where'] = $where;
        $param['order'] = "TB1.id desc";
        $param['group'] = "";
        $param['limit'] = "";
        $param['pagesize'] = $size;
        $param['page'] = $page;
        $param['join'] = "left join product";
        $param['on'] = "TB1.pid = TB2.id";
        $data = db_query_hash("fg_product", $param);
        if ($data['result']) {
            foreach ($data['result'] as $key => $value) {
                $vote_option_json = json_decode($value['vote_option_json'], 1);
                if ($vote_option_json) {
                    foreach ($vote_option_json as $key_json => $value_json) {
                        $value_json['topicintro'] = $fg_vote_option_typearr[$value_json['topicintro']];
                        $value_json['topicstar'] = $this->fg_images . basename($value_json['topicstar']);
                        $vote_option_json_array[$key_json] = $value_json;
                    }
                }
                if (empty($value['cname']))
                    $value['cname'] = $value['name'];
                $value['niceimg'] = $this->fg_images . basename($value['niceimg'], ".gif") . ".png";
                $value['imgurl'] = $value['cover_image1'];
                $value['type_name'] = @$brandarr[$value['brand']];
                $value['vote_option_json'] = $vote_option_json_array;
                $result[$key] = $value;
            }
            $data['result'] = $result;
        }

        return $data;
    }

    function get_new_fg_report($h_param = array()) {
        $result = array();
        $h_param['is_new'] = 1;
        $h_param['page'] = 1;
        $h_param['size'] = 12;
        $data = $this->get_fg_report($h_param);

        if ($data['result']) {
            foreach ($data['result'] as $key => $value) {

                $product_data = $this->Product->getProductById($value['pid']);
                if ($product_data)
                    $result[$key] = $product_data;
            }
        }
        return $result;
    }

    function get_fg_report($h_param = array()) {
        $result = array();
        $vote_option_json_array = array();

        $pid = intval($h_param['pid']) ? intval($h_param['pid']) : 0;
        $id = intval($h_param['id']) ? intval($h_param['id']) : 0;
        $pic_num = intval($h_param['pic_num']) ? intval($h_param['pic_num']) : 0;


        $page = intval($h_param['page']) ? intval($h_param['page']) : 1;
        $size = intval($h_param['size']) ? intval($h_param['size']) : 5;

        $is_new = intval($h_param['is_new']) ? intval($h_param['is_new']) : 0;




        $where = "TB1.status=1 and TB2.pid>0";
        if ($id && $pid) {
            $where .= " and TB1.id=" . $id . " and TB2.pid=" . $pid;
        } elseif ($id) {
            $where .= " and TB1.id=" . $id;
        } elseif ($pid) {
            $where .= " and TB2.pid=" . $pid;
        }

        $orderby = "TB1.msgdate desc,TB1.id desc";
        if ($pic_num) {
            $where .= " and TB1.pic_num>=" . $this->pic_num;
            $orderby = "TB1.pic_num desc,TB1.id desc";
        }

        $db = 'fg_report';
        $group = "";
        if ($is_new) {
            $group = "TB1.num";
            $where .= " and TB2.status=1";
            $db = "(select * from fg_report where status=1 order by id desc)";
        }

        $fg_vote_option_typearr = array();
        $fg_vote_option_type = $this->Lookup->getLookup('fg_vote_option_type');
        if ($fg_vote_option_type['result']) {
            foreach ($fg_vote_option_type['result'] as $value) {
                $fg_vote_option_typearr[$value['id']] = $value['cname'];
            }
        }


        $param = array();
        $param['field'] = "TB1.id,TB1.num,TB1.msgname,TB1.msgavatar,TB1.msgskin,TB1.msgage,TB1.msg,TB1.msgitem,TB1.vote_option_json,TB1.fb_share_num_base,(TB1.likes+TB1.likes_num_base) as likes,TB1.status,TB2.pid";
        $param['where'] = $where;
        $param['order'] = $orderby;
        $param['group'] = $group;
        $param['limit'] = "";
        $param['pagesize'] = $size;
        $param['page'] = $page;
        $param['join'] = "right join fg_product";
        $param['on'] = "TB2.num = TB1.num";
        $data = db_query_hash($db, $param);

        if ($data['result']) {
            foreach ($data['result'] as $key => $value) {
                $vote_option_json = json_decode($value['vote_option_json'], 1);
                if ($vote_option_json) {
                    foreach ($vote_option_json as $key_json => $value_json) {
                        $value_json['msgintro'] = $fg_vote_option_typearr[$value_json['msgintro']];
                        $value_json['msgstar'] = $this->fg_images . basename($value_json['msgstar']);
                        $vote_option_json_array[$key_json] = $value_json;
                    }
                }
                $value['vote_option_json'] = $vote_option_json_array;

                $pat = '/<img(.*?)src="(.*?)"(.*?)>/i';
                $replacement = '<a href="${2}" title="" class="img_lightbox"><img${1}src="${2}"${3}></a>';
                $value['msg'] = preg_replace($pat, $replacement, $value['msg']);

                $result[$key] = $value;
            }
            $data['result'] = $result;
        }

        return $data;
    }

    function get_fg_hilite_left_product() {
        $result = array();

        $param = array();
        $param['field'] = "";
        $param['where'] = "";
        $param['order'] = "id desc";
        $param['group'] = "";
        $param['limit'] = "1";
        $param['pagesize'] = "";
        $param['page'] = "";
        $param['join'] = "";
        $param['on'] = "";
        $data = db_query_hash("fg_hilite_left_product", $param);

        if ($data['result']) {


            $h_param = array();
            $h_param['pid'] = $data['result'][0]['pid'];
            $data = $this->get_fg_product($h_param);

            if ($data['result'])
                $result = $data['result'];
        }

        return $result;
    }

    function get_fg_hilite_middle_product() {
        $result = array();

        $param = array();
        $param['field'] = "";
        $param['where'] = "";
        $param['order'] = "id desc";
        $param['group'] = "";
        $param['limit'] = "1";
        $param['pagesize'] = "";
        $param['page'] = "";
        $param['join'] = "";
        $param['on'] = "";
        $data = db_query_hash("fg_hilite_middle_product", $param);

        if ($data['result']) {


            $h_param = array();
            $h_param['pid'] = $data['result'][0]['pid'];
            $h_param['pic_num'] = 1;
            $h_param['rid'] = $data['result'][0]['rid'];
            $result = $this->get_fg_product_report($h_param);
        }

        return $result;
    }

    function get_fg_hilite_category() {
        $result = array();
        $param = array();
        $param['field'] = "TB2.cname,TB1.product_layout_category as cid";
        $param['where'] = "TB2.status=1 and TB1.status=1";
        $param['order'] = "TB1.sequence asc,TB1.id asc";
        $param['group'] = "";
        $param['limit'] = 6;
        $param['pagesize'] = "";
        $param['page'] = "";
        $param['join'] = "left join product_layout_category";
        $param['on'] = "TB1.product_layout_category = TB2.id";
        $data = db_query_hash('fg_hilite_category', $param);
        if ($data['result'])
            $result = $data['result'];
        return $result;
    }

    function get_all_product_by_category() {

        $result = array();
        $param = array();
        $param['field'] = "TB1.id";
        $param['where'] = "TB2.is_report=1 AND TB1.product_layout_category=" . $cid;
        $param['order'] = "TB1.id desc";
        $param['group'] = "";
        $param['limit'] = 30;
        $param['pagesize'] = "";
        $param['page'] = "";
        $param['join'] = "right join fg_product";
        $param['on'] = "TB1.id = TB2.pid";
        $data = db_query_hash('product', $param);
        if ($data['result']) {
            foreach ($data['result'] as $value) {
                $h_param = array();
            }
        }

//echo '<pre>' . var_export($result, 1) . '</pre>';

        return $result;
    }

    function get_fg_product_by_category($h_param = array()) {

        $cid = intval($h_param['cid']);

        if (!$cid)
            return array();

        $result = array();
        $param = array();
        $param['field'] = "auto_hilite";
        $param['where'] = "status=1 and product_layout_category=" . $cid;
        $param['order'] = "";
        $param['group'] = "";
        $param['limit'] = "";
        $param['pagesize'] = "";
        $param['page'] = "";
        $param['join'] = "";
        $param['on'] = "";
        $data = db_query_hash('fg_hilite_category', $param);
        if ($data['result']) {
            if (!empty($data['result'][0]['auto_hilite'])) {
                $rid_array = explode(",", $data['result'][0]['auto_hilite']);

                if ($rid_array) {
                    foreach ($rid_array as $key => $value) {
                        $param = array();
                        $param['field'] = "";
                        $param['where'] = "status=1 and id=" . $value;
                        $param['order'] = "";
                        $param['group'] = "";
                        $param['limit'] = "";
                        $param['pagesize'] = "";
                        $param['page'] = "";
                        $param['join'] = "";
                        $param['on'] = "";
                        $data_fg_report = db_query_hash('fg_report', $param);
                        if ($data_fg_report['result']) {

                            $h_param = array();
                            $h_param['num'] = $data_fg_report['result'][0]['num'];
                            $data_fg_product = $this->get_fg_product($h_param);


                            if ($data_fg_product['result']) {

                                $result_p['product'] = $data_fg_product['result'];
                                $result_p['report'] = $data_fg_report['result'];

                                $result[$key] = $result_p;
                            }
                        }
                    }
                }
            }
        }


        /*
          $result = array();
          $param = array();
          $param['field'] = "TB1.id";
          $param['where'] = "TB2.is_report=1 AND TB1.product_layout_category=" . $cid;
          $param['order'] = "TB1.id desc";
          $param['group'] = "";
          $param['limit'] = 30;
          $param['pagesize'] = "";
          $param['page'] = "";
          $param['join'] = "right join fg_product";
          $param['on'] = "TB1.id = TB2.pid";
          $data = db_query_hash('product', $param);
          if ($data['result']) {
          foreach ($data['result'] as $value) {
          $h_param = array();

          $h_param['pid'] = $value['id'];
          $h_param['pic_num'] = 1;
          $result[] = $this->get_fg_product_report($h_param);
          }
          }
          if ($result) {
          foreach ($result as $key => $value) {
          if (empty($value['report']))
          unset($result[$key]);
          }
          }
         */
//echo '<pre>' . var_export($result, 1) . '</pre>';

        return $result;
    }

    function get_fg_product_report($h_param = array()) {

        $pid = intval($h_param['pid']);
        $pic_num = $h_param['pic_num'];

        if (!$pid)
            return array();

        $h_param = array();
        $report = array();
        $product = array();
        $result = array();
        $h_param['pid'] = $pid;

        $product_data = $this->get_fg_product($h_param);

        if ($product_data['result']) {
            $product = $product_data['result'];
            $h_param['page'] = 1;
            $h_param['size'] = 1;
            if ($pic_num)
                $h_param['pic_num'] = 1;
            $report_data = $this->get_fg_report($h_param);
            if ($report_data['result'])
                $report = $report_data['result'];
        }

        $result['product'] = $product;
        $result['report'] = $report;

        return $result;
    }

    function get_fg_hilite_user() {
        $result = array();
        $param = array();
        $param['field'] = "";
        $param['where'] = "status=1";
        $param['order'] = "sequence asc,id asc";
        $param['group'] = "";
        $param['limit'] = 6;
        $param['pagesize'] = "";
        $param['page'] = "";
        $param['join'] = "";
        $param['on'] = "";
        $data = db_query_hash('fg_hilite_user', $param);
        if ($data['result']) {
            foreach ($data['result'] as $key => $value) {
                $value['report'] = array();
                if ($value['rid']) {
                    $h_param = array();
                    $h_param['id'] = $value['rid'];
                    $h_param['page'] = 1;
                    $h_param['size'] = 1;
                    $report_data = $this->get_fg_report($h_param);


                    if ($report_data['result']) {
                        $value['report'] = $report_data['result'][0];
                        $h_param = array();
                        $value['report']['pid'] = "";
                        $h_param['num'] = $report_data['result'][0]['num'];
                        $product_data = $this->get_fg_product($h_param);
                        if ($product_data['result']) {
                            $value['report']['member_type'] = $product_data['result'][0]['member_type'];
                            $value['report']['pid'] = $product_data['result'][0]['pid'];
                        }
                    }
                }
                $result[$key] = $value;
            }
        }

        return $result;
    }

    function get_fg_blog_list($h_param = array()) {
        $page = intval($h_param['page']) ? intval($h_param['page']) : 1;
        $size = intval($h_param['size']) ? intval($h_param['size']) : 12;
        $id = intval($h_param['id']) ? intval($h_param['id']) : 0;
        $where = "post_status=1 and is_enabled =1 ";
        if ($id) {
            $where .=" and id=" . $id;
        }

        $param1 = array();
        $param1['field'] = "";
        $param1['where'] = $where;
        $param1['order'] = "id desc"; //"id asc"
        $param1['group'] = "";
        $param1['limit'] = "";
        $param1['pagesize'] = $size;
        $param1['page'] = $page;
        $param1['join'] = "";
        $param1['on'] = "";
        $data_fg_blog = db_query_hash('fg_blog', $param1);

        return $data_fg_blog;
    }

}

?>